<?php

class class_shoppingcart
{
	//Khai bao bien
		
	
	function class_shoppingcart()	
	{
		//Khoi tao bien
			
	}
	

	function total_items()
	{
		global $dbconn, $arr_id;
		//echo "arr_id: $arr_id<br>";
		$result = 0;
		if ($arr_id == "" || !isset($arr_id) || $arr_id == NULL){
			$result = 0;
			//echo "result: $result<br>";
		}else{
			$sql = "select count(id) from sanpham where id in ($arr_id) ";
			//echo "sql: $sql<br>";
			$result = get_values($sql);
		}
		if ($result < 1){
			$result = 0;
		}
		return "$result";
	}//end total_items
	
	function viewcart()
	{
		global $dbconn;
		global 
			$return_page, 
			$arr_muatiep, 
			$langid, 
			$arr_viewcart, 
			$arr_viewcart_template,
			$arr_sanpham_text,
			$arr_nut,
			$arr_nut_over
			;
		global $arr_id; // nam trong file include
		global $session_arr_cart; //array chua cart content
		
		$return_page_luu = $return_page;

		
		//print_r ($arr_id);echo "<br>";
		//Main HTML
		$str_htm = loadHtml($arr_viewcart_template[$langid], "");
		
		//Template
		$list = "";
		$list_products = "";
		$list_begin = loadHtml("../htmls_templates/tmp_list_viewcart.htm", "");
		$list_content = loadHtml("../htmls_templates/tmp_list_viewcart_content.htm", "");
		//echo htmlspecialchars($list_content);
		$list_end = "</table>";


		if ($arr_id == ""){		
			$msg_item = "<strong>".$arr_viewcart[$langid][4]."</strong>";		
			$str_htm = ereg_replace("@list_products@",$msg_item,$str_htm);
		}
		else{
			$msg_item = "";
					
			//Data
			$strSql= "select * from danhba where id in ($arr_id)";
			//echo "$strSql<br>";
			$rs= new db_query($dbconn,$strSql);
			$i = 0;
			$stt=1;
			while ($rs->getrow())
			{
				$content = $list_content;			
				$fld_id = $rs->row["id"];
				$company_name = $rs->row["company_name"];
				$address = $rs->row["address"];
				$phone = $rs->row["phone"];


				$gia = $rs->row["gia"];
				$donvi = $rs->row["donvi"];
				//echo "donvi: $donvi<br>";
				if($donvi < 2){//VND
					$gia = number_format($gia,0,".",",") . " VND";
				}else{//USD
					$gia = "$".number_format($gia,2,".",",");
				}		
				
				$img1 = $rs->row["img1"];
				if($img1 != ""){
					$img_fullpath = "../uploads/sanpham/".$img1;
					if (file_exists($img_fullpath)){
						$popsize=getimagesize($img_fullpath) ;
						$img_width = htmlspecialchars($popsize[0]);
						$img_height = htmlspecialchars($popsize[1]);
						//echo "img_width: $img_width - img_height: $img_height<br>";
						if ($img_width > CART_IMAGES_SMALL_WIDTH){
							$img_tile = ceil((CART_IMAGES_SMALL_WIDTH*100)/$img_width);
							$img_width = CART_IMAGES_SMALL_WIDTH;
							$img_height = ceil(($img_tile*$img_height)/100);
							//echo "img_tile: $img_tile - img_width: $img_width - img_height: $img_height<br>";
						}																
						$value_replacement = "<img src='".$img_fullpath."' width='$img_width' height='$img_height' border=0>";
						$link = "sanpham_chitiet.php?catid=$catid&subcatid=$subcatid&proid=$fld_id&langid=$langid&file_link=sanpham.php";
						$value_replacement = "<a href=\"$link\">$value_replacement</a>";
					}
				}else{
					$value_replacement = "&nbsp;";
					$img1 = "";
				}
								

				$content = ereg_replace("@Code@", "$maso",$content);
				$content = ereg_replace("@image@","$value_replacement",$content);
				$content = ereg_replace("@img1@","$img1",$content);
				$content = ereg_replace("@soluong@","$soluong",$content);
				
				$content = ereg_replace("@company_name@","$company_name",$content);
				$content = ereg_replace("@address@","$address",$content);
				$content = ereg_replace("@phone@","$phone",$content);
				$content = ereg_replace("@stt@","$stt",$content);

				$content = ereg_replace("@gia@","$gia",$content);

				$sql = "select sanpham_cat from sanpham where id = '$fld_id'";
				
				$catid = get_values($sql);
				$content=ereg_replace("@catid@","$catid",$content);
				$content=ereg_replace("@proid@",$fld_id,$content);
				
				
				//CSS
				if ($i==0){$class="listitem_box_0";$i=1;} else {$class="listitem_box_1";$i=0;}	
				$content=ereg_replace("@class@",$class,$content);		
												
				$list.= $content;
				$stt++;
			}
		}//end if
		//echo htmlspecialchars($list);
	 
		$list_products = $list_begin.$list.$list_end;
		
		$list_products = ereg_replace("@txt_maso@", $arr_viewcart[$langid][0], $list_products);
		$list_products = ereg_replace("@txt_mathang@", $arr_viewcart[$langid][1], $list_products);
		$list_products = ereg_replace("@txt_soluong@", $arr_viewcart[$langid][2], $list_products);
		$list_products = ereg_replace("@action@", $arr_viewcart[$langid][3], $list_products);
		$list_products = ereg_replace("@txt_maso@", $arr_viewcart[$langid][4], $list_products);
		$list_products = ereg_replace("@txt_gia@", $arr_viewcart[$langid][5], $list_products);
		
		$str_htm = ereg_replace("@list_products@",$list_products,$str_htm);
		
		//mua tiep
		$return_page = ereg_replace("__", "&", $return_page);
		if($return_page != ""){
			//echo "return_page: $return_page<br>";
			$link_muatiep = $return_page;
		}else{
			$link_muatiep = $arr_muatiep[$langid];
		}
		
		//nut
		$nut_muatiep = nut_over($arr_nut[$langid][3],$arr_nut_over[$langid][3], "nut_muatiep", $link_muatiep);
		$str_htm=ereg_replace("<!--nut_muatiep-->", $nut_muatiep, $str_htm);		

		$str_htm = ereg_replace("@return_page@",$return_page_luu,$str_htm);
		
		return $str_htm;
	}//end viewcart
	
	
	function sendcart()
	{
		global $order_note, $debug, $cif_email, $arr_viewcart_send, $langid;

		$NgayThang  = mktime (date("H")+$time_zone,date("i"),date("s"),date("m"),date("d"),date("Y"));
		$today = date("d/m/Y",$NgayThang);
		$ngay = date("Y-m-d",$NgayThang);
		$gio = date("H:i:s",$NgayThang);
		$ngaygio = "$today $gio";
		
		$donhang = $this->donhang();
		$khachhang = $this->thongtin_khachhang();
		
		$order_body = loadHtml("../htmls/send_order.htm","");
		$order_body = ereg_replace("@order_information@", "$donhang", $order_body);
		$order_body = ereg_replace("@customer_information@", "$khachhang", $order_body);
		$order_body = ereg_replace("@orderdate@", "$ngaygio", $order_body);
		$order_body = ereg_replace("@note@", "$order_note", $order_body);
		
		if ($debug){
			echo $order_body;
		}
		
		$subject="Order from website - $ngaygio";
		$subject1=STORE_NAME." - Order - $ngaygio";
		
		$mail_to_1 = STORE_OWNER_EMAIL_ADDRESS;
		$mail_to_2 = SEND_EXTRA_ORDER_EMAILS_TO;
		$mail_to_test = "info@trawenic.com";
		
		//tep_mail_html($to_name, $to_email_address, $email_subject, $email_text, $from_email_name, $from_email_address)
		tep_mail_html($mail_to_1,$mail_to_1,$subject,$order_body,$cif_email,$cif_email);
		tep_mail_html($mail_to_test,$mail_to_test,$subject1,$order_body,$cif_email,$cif_email);
		if ($mail_to_2!="" && $mail_to_2 != "SEND_EXTRA_ORDER_EMAILS_TO"){
			tep_mail_html($mail_to_2,$mail_to_2,$subject,$order_body,$cif_email,$cif_email);
		}
		
		
		//Xoa Session
		session_unregister('session_arr_cart');

		
		$html = loadHtml($arr_viewcart_send[$langid],"");
		return $html;
		
	}//end sendcart
	
	function donhang()
	{
		global $maso, $ten, $gia, $soluong, $debug, $company_name, $address, $phone;
		$company_name_cnt = count($company_name);
		if ($company_name_cnt > 0) 
		{
			$list = "";
			$content = "";
			$donhang = loadHtml("../htmls/send_cart_content.htm","");
			
			for ($i = 0; $i < $company_name_cnt; ++$i) 
			{	
				$stt=$i+1;
				$maso_str = $maso[$i];
				$ten_str = $ten[$i];							   
				$soluong_str = $soluong[$i];
				$gia_str = $gia[$i];
				
				$congty = $company_name[$i];
				$diachi = $address[$i];
				$dienthoai = $phone[$i];
				
				
				if($ten_str != ""){
					$hinh = "<img src='http://".HOST_DOMAIN_NAME.ROOT_WS."uploads/sanpham/".$ten_str."' width='80'>";
				}else{
					$hinh = "&nbsp;";
				}
				
				$content = loadHtml("../htmls/send_cart_content_items.htm",$msg);
				$content = ereg_replace("@code@",$maso_str,$content);
				$content = ereg_replace("@items@",$hinh,$content);
				$content = ereg_replace("@quantity@",$soluong_str,$content);
				$content = ereg_replace("@gia@",$gia_str,$content);
				
				$content = ereg_replace("@company_name@",$congty,$content);
				$content = ereg_replace("@address@",$diachi,$content);
				$content = ereg_replace("@phone@",$dienthoai,$content);
				$content = ereg_replace("@stt@","$stt",$content);
				
				$list .= $content;
				
			} // end for
			$donhang = ereg_replace("<!--list-->", $list, $donhang);			
		}
		else
		{
			$donhang = "No items seleted.";
		}//end if $maso_cnt >0
		if ($debug){
			//echo "$donhang<br>";
		}
		
		return $donhang;
		
	}//end donhang
	
	function thongtin_khachhang()
	{
		global $arr_cif, $debug;
		$html = "";
		$html = loadHtml("../htmls/send_customer_information.htm","");
		if (count($arr_cif) > 0){
			foreach ($arr_cif as $key=>$value){
				//$$key = $value;	//gan gia tri vao bien
				$pattern = "@$key@";
				$replacement = "$value";
				$html = ereg_replace($pattern, $replacement, $html);
			}//end for
		}
		if ($debug){
			//echo $html;
		}
		return $html;	
	} // end thongtin_khachhang
	
}//end class_shoppingcart

?>
